package com.agua.enturno.vista.paginas.adm.agendas.dias_no_laborales;

import com.agua.enturno.excepciones.ExcepcionDeReglaDelNegocio;
import com.agua.enturno.servicios.AgendaService;
import com.agua.enturno.vista.componentes.AjaxConfirmLink;
import com.agua.enturno.vista.dto.DiaNoLaboralDTO;
import com.agua.enturno.vista.modelos.LocalDateModel;
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.markup.html.form.AjaxButton;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.markup.html.list.ListItem;
import org.apache.wicket.markup.html.list.ListView;
import org.apache.wicket.markup.html.panel.FeedbackPanel;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.PropertyModel;
import org.apache.wicket.spring.injection.annot.SpringBean;
import org.joda.time.LocalDate;

import java.util.List;

public class DiasNoLaboralesPanel extends Panel {

	private static final long serialVersionUID = 1L;
	
	@SpringBean
	private AgendaService agendaService;

	private DiaNoLaboralDTO diaNoLaboral = new DiaNoLaboralDTO();
	
	public DiasNoLaboralesPanel(String id, final IModel<List<DiaNoLaboralDTO>> diasNoLaboralesModel, final IModel<Long> modelAgendaId) {
		super(id, modelAgendaId);
		this.setOutputMarkupId(true);
		
		@SuppressWarnings("rawtypes")
		Form form = new Form("agregar-dia-no-laboral-form");
        add(form);
    	form.add(new TextField<String>("fecha", new LocalDateModel(new PropertyModel<LocalDate>(this, "diaNoLaboral.fecha"))));
    	form.add(new TextField<String>("motivo", new PropertyModel<String>(this, "diaNoLaboral.motivo")));
    	form.add(new AjaxButton("agregar-dia") {
			
			private static final long serialVersionUID = 1L;

			@Override
			protected void onSubmit(AjaxRequestTarget target, Form<?> form) {
				
				try {
					
					diaNoLaboral.setId(agendaService.agregarDiaNoLaboral(modelAgendaId.getObject(), diaNoLaboral));
					
					diasNoLaboralesModel.getObject().add(diaNoLaboral);
					diaNoLaboral = new DiaNoLaboralDTO();
					
				} catch (ExcepcionDeReglaDelNegocio e) {
					error(e.getMessage());
				}
				target.add(DiasNoLaboralesPanel.this);
			}
			
			@Override
			protected void onError(AjaxRequestTarget target, Form<?> form) {
				target.add(DiasNoLaboralesPanel.this);
			}
		});
    	
    	add(new FeedbackPanel("feedback"));
    	
    	add(new ListView<DiaNoLaboralDTO>("dias-no-laborales", diasNoLaboralesModel) {

			private static final long serialVersionUID = 1L;

			@Override
			protected void populateItem(ListItem<DiaNoLaboralDTO> item) {
				
				final DiaNoLaboralDTO diaNoLaboral = item.getModelObject();
				
				item.add(new Label("fecha-tabla", diaNoLaboral.getFecha().toString("dd/MM/yyyy")));
				item.add(new Label("motivo-tabla", diaNoLaboral.getMotivo()));

				item.add(new AjaxConfirmLink("eliminar-dia", "¿Realmente desea eliminar el día no laboral?") {
					private static final long serialVersionUID = 1L;

					@Override
					public void onClick(AjaxRequestTarget target) {
						
						try {
							agendaService.quitarDiaNoLaboral(modelAgendaId.getObject(), diaNoLaboral.getId());
							diasNoLaboralesModel.getObject().remove(diaNoLaboral);
							
						} catch (ExcepcionDeReglaDelNegocio e) {
							error(e.getMessage());
						}
						target.add(DiasNoLaboralesPanel.this);
					}
				});
			}
		});
		
	}

}
